package com.bridgeQQ.client.control.connection.bs
{
	import com.bridgeQQ.client.control.connection.bs.connection.BsConnection;
	import com.bridgeQQ.client.core.client.IConnectionClient;
	import com.bridgeQQ.client.events.SendEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.messaging.Consumer;
	import mx.messaging.Producer;
	
	public class BsClient extends EventDispatcher implements IConnectionClient
	{
		
		private var handers:Object = {};
		
		private var bsConnection:BsConnection;

		public function BsClient(outboud:Producer, inbound:Consumer)
		{
			bsConnection = new BsConnection(outboud, inbound);
			bsConnection.addEventListener(SendEvent.SUCCESS, onSendResult);
			bsConnection.addEventListener(SendEvent.ERROR, onSendResult);
			bsConnection.addEventListener(SendEvent.TIME_OUT, onSendResult);
		}
		
		public function registerHandler(name:String, handler:Function):void
		{
			handers[name] = handler;
			bsConnection.handlers = handers;
		}
		
		public function send(userID:String, handler:String, ...params):String
		{	
			params.unshift(userID);
			params.unshift(handler);
			return bsConnection.send.apply(null, params);
		}
		
		public function set userID(value:String):void
		{
			bsConnection.userID = value;
		}
		
		private function onSendResult(sevent:SendEvent):void
		{
			sevent.stopPropagation();
			this.dispatchEvent(SendEvent(sevent.clone()));
		}
	}
}